﻿#include "opcua_qthread.h"
#include <QMutexLocker>

//静态成员初始化（设备ID列表，仅cpp可见）
const QStringList OpcUA_QThread::s_valveID_OnlyRead = {
    u8"omron.nj101.阀门输出1", u8"omron.nj101.阀门输出2", u8"omron.nj101.阀门输出3",
    u8"omron.nj101.阀门输出4", u8"omron.nj101.阀门输出5", u8"omron.nj101.阀门输出6",
    u8"omron.nj101.阀门输出7", u8"omron.nj101.阀门输出8", u8"omron.nj101.阀门输出9",
    u8"omron.nj101.阀门输出10", u8"omron.nj101.阀11"
};

const QStringList OpcUA_QThread::s_valveID_ReadAndWrite = {
    u8"omron.nj101.氮气阀上位机", u8"omron.nj101.硫化氢1_32阀上位机",
    u8"omron.nj101.硫化氢1_52阀上位机", u8"omron.nj101.硫化氢2_32阀上位机",
    u8"omron.nj101.硫化氢2_52阀上位机", u8"omron.nj101.源瓶1line阀上位机",
    u8"omron.nj101.源瓶1Run阀上位机", u8"omron.nj101.源瓶2Line阀上位机",
    u8"omron.nj101.源瓶2Run阀上位机", u8"omron.nj101.换源气阀上位机"
};

const QStringList OpcUA_QThread::s_otherValveID_OnlyRead = {
    u8"omron.nj101.规阀", u8"omron.nj101.新增真空泵", u8"omron.nj101.新增真空泵_MP启动",
    u8"omron.nj101.新增真空泵_BP启动", u8"omron.nj101.炉子开启", u8"omron.nj101.指示灯1",
    u8"omron.nj101.指示灯", u8"omron.nj101.指示灯2"
};

const QStringList OpcUA_QThread::s_flowmeterID_OnlyRead = {
    u8"omron.nj101.流量读数1", u8"omron.nj101.流量读数2", u8"omron.nj101.流量读数3",
    u8"omron.nj101.流量读数4", u8"omron.nj101.流量读数5", u8"omron.nj101.流量读数6",
    u8"omron.nj101.流量读数7", u8"omron.nj101.流量读数8", u8"omron.nj101.流量读数9"
};

const QStringList OpcUA_QThread::s_flowmeterID_ReadAndWrite = {
    u8"omron.nj101.流量1设定", u8"omron.nj101.流量2设定", u8"omron.nj101.流量3设定",
    u8"omron.nj101.流量4设定", u8"omron.nj101.流量5设定", u8"omron.nj101.流量6设定",
    u8"omron.nj101.流量7设定", u8"omron.nj101.流量8设定", u8"omron.nj101.流量9设定"
};

const QStringList OpcUA_QThread::s_pressuremeterID_OnlyRead = {
    u8"omron.nj101.压力读数1", u8"omron.nj101.压力读数2"
};

const QStringList OpcUA_QThread::s_pressuremeterID_ReadAndWrite = {
    u8"omron.nj101.压力设定1", u8"omron.nj101.压力设定2"
};

const QStringList OpcUA_QThread::s_MKSID_OnlyRead = {
    u8"omron.nj101.MKS反馈1", u8"omron.nj101.MKS反馈2"
};

const QStringList OpcUA_QThread::s_MKSID_ReadAndWrite = {
    u8"omron.nj101.MKS设定1", u8"omron.nj101.MKS设定2", u8"omron.nj101.MKS控制"
};

const QStringList OpcUA_QThread::s_temperatureID_OnlyRead = {
    u8"omron.nj101.实际温度1", u8"omron.nj101.实际温度2"
};

const QStringList OpcUA_QThread::s_temperatureID_ReadAndWrite = {
    u8"omron.nj101.温控启动", u8"omron.nj101.上位机温控启动", u8"omron.nj101.温度设定值动态", u8"omron.nj101.温度设定值动态1"
};

const QStringList OpcUA_QThread::s_otherDataID_OnlyRead = {
    u8"omron.nj101.腔外壳体温度", u8"omron.nj101.压力实数9", u8"omron.nj101.水温1",
    u8"omron.nj101.水温2", u8"omron.nj101.水压", u8"omron.nj101.水流",
    u8"omron.nj101.Loop1PV", u8"omron.nj101.Loop2PV"
};

//构造函数
OpcUA_QThread::OpcUA_QThread(QObject *parent)
    : QThread(parent), m_client(nullptr), m_running(true)
{
    m_client = new Open62541();

    //注册 QVector<int16_t> 为 Qt 元类型，才能确保connect正确连接信号与槽
    qRegisterMetaType<QVector<int16_t>>("QVector<int16_t>");
    qRegisterMetaType<QVector<bool>>("QVector<bool>");
    qRegisterMetaType<int32_t>("int32_t");
}

//析构函数
OpcUA_QThread::~OpcUA_QThread()
{
    m_running = false;
    m_condition.wakeOne();  //唤醒等待的线程
    wait();                 //等待线程退出
    if (m_client) {
        delete m_client;    //释放客户端资源
        m_client = nullptr;
    }
}

//初始化OPC UA连接
void OpcUA_QThread::init(const QString &endpointUrl)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::Init;
    task.endpointUrl = endpointUrl;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取单个阀门状态
void OpcUA_QThread::readValveStatus(int valveChannel)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadValveStatus;
    task.channel = valveChannel;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取所有阀门状态
void OpcUA_QThread::readAllValveStatus()
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadAllValveStatus;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入单个阀门状态
void OpcUA_QThread::writeValveStatus(int valveChannel, bool status)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteValveStatus;
    task.channel = valveChannel;
    task.boolValue = status;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入所有阀门状态
void OpcUA_QThread::writeAllValveStatus(const QVector<bool> &status)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteAllValveStatus;
    task.boolVector = status;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取单个流量值
void OpcUA_QThread::readFlowRate(int flowmeterChannel)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadFlowRate;
    task.channel = flowmeterChannel;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取所有流量值
void OpcUA_QThread::readAllFlowRate()
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadAllFlowRate;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入单个流量值
void OpcUA_QThread::writeFlowRate(int flowmeterChannel, int16_t inputValue)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteFlowRate;
    task.channel = flowmeterChannel;
    task.int16Value = inputValue;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入所有流量值
void OpcUA_QThread::writeAllFlowRate(const QVector<int16_t> &inputValue)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteAllFlowRate;
    task.int16Vector = inputValue;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取单个压力值
void OpcUA_QThread::readPressure(int pressuremeterChannel)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadPressure;
    task.channel = pressuremeterChannel;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取所有压力值
void OpcUA_QThread::readAllPressure()
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadAllPressure;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入单个压力值
void OpcUA_QThread::writePressure(int pressuremeterChannel, int16_t inputValue)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WritePressure;
    task.channel = pressuremeterChannel;
    task.int16Value = inputValue;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入所有压力值
void OpcUA_QThread::writeAllPressure(const QVector<int16_t> &inputValue)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteAllPressure;
    task.int16Vector = inputValue;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取MKS设备状态
void OpcUA_QThread::readMKS()
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadMKS;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入MKS设备参数
void OpcUA_QThread::writeMKS(int16_t pressure, int16_t openingAmplitude, int16_t switchMode)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteMKS;
    task.mksPressure = pressure;
    task.mksOpening = openingAmplitude;
    task.mksMode = switchMode;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取温度值
void OpcUA_QThread::readTemperature()
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadTemperature;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//写入温度参数
void OpcUA_QThread::writeTemperature(bool status1, bool status2, int16_t temperature1, int16_t temperature2)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteTemperature;
    task.tempStatus1 = status1;
    task.tempStatus2 = status2;
    task.temp1 = temperature1;
    task.temp2 = temperature2;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取其他阀门状态
void OpcUA_QThread::readOtherValveStatus(int valveChannel)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadOtherValveStatus;
    task.channel = valveChannel;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//读取其他数据值
void OpcUA_QThread::readOtherDataValue(int dataChannel)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::ReadOtherDataValue;
    task.channel = dataChannel;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

void OpcUA_QThread::readDataToHomePage()
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::readDataToHomePage;
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

void OpcUA_QThread::writeDataFromHomePage(const QVector<int16_t> &flowValues, const QVector<int16_t> &pressureValues, const QVector<bool> &valves)
{
    QMutexLocker locker(&m_mutex);
    OpcUA_QThread_Task task;
    task.type = OpcUA_QThread_Task::WriteDataFromHomePage;  // 需要在Type枚举中添加该类型
    task.int16Vector = flowValues;         // 存储流量值
    task.int16Vector2 = pressureValues;    // 需要在结构体中添加该成员
    task.boolVector = valves;         // 存储阀门状态
    m_tasks.enqueue(task);
    m_condition.wakeOne();
}

//线程运行函数（处理所有任务）
void OpcUA_QThread::run()
{
    while (m_running)
    {
        OpcUA_QThread_Task task;
        {
            QMutexLocker locker(&m_mutex);
            //如果任务队列为空，等待新任务
            if (m_tasks.isEmpty()) {
                m_condition.wait(&m_mutex);
            }
            //检查线程是否需要退出
            if (!m_running) break;
            //取出任务
            task = m_tasks.dequeue();
        }

        //根据任务类型处理
        switch (task.type)
        {
            case OpcUA_QThread_Task::Init: {
            //初始化
                bool success = m_client->Open62541_Init(task.endpointUrl);
                emit signals_initResult(success);
                break;
            }

            case OpcUA_QThread_Task::ReadValveStatus: {
            //读单个电磁阀值
                bool result = false;
                bool success = false;
                if (task.channel > 0 && task.channel <= s_valveID_OnlyRead.size()) {
                    success = m_client->Open62541_ReadData(2, s_valveID_OnlyRead[task.channel - 1], result);
                }
                emit signals_valveStatusRead(task.channel, result, success);
                break;
            }

            case OpcUA_QThread_Task::ReadAllValveStatus: {
            //读所有电磁阀值
                QVector<bool> result(s_valveID_OnlyRead.size());
                QMap<QString, QVariant> data;
                bool success = m_client->Open62541_batchReading(2, s_valveID_OnlyRead, data);
                if (success) {
                    for (int i = 0; i < s_valveID_OnlyRead.size(); ++i) {
                        result[i] = data.value(s_valveID_OnlyRead[i]).toBool();
                    }
                }
                emit signals_allValveStatusRead(result, success);
                break;
            }

            case OpcUA_QThread_Task::WriteValveStatus: {
            //写单个电磁阀值
                bool success = false;
                if (task.channel > 0 && task.channel <= s_valveID_ReadAndWrite.size()) {
                    success = m_client->Open62541_WriteData(2, s_valveID_ReadAndWrite[task.channel - 1], task.boolValue);
                }
                emit signals_valveStatusWritten(task.channel, success);
                break;
            }

            case OpcUA_QThread_Task::WriteAllValveStatus: {
            //写所有电磁阀值
                bool success = true;
                int max = qMin(task.boolVector.size(), s_valveID_ReadAndWrite.size());
                for (int i = 0; i < max; ++i) {
                    if (!m_client->Open62541_WriteData(2, s_valveID_ReadAndWrite[i], task.boolVector[i])) {
                        success = false;
                        break;
                    }
                }
                emit signals_allValveStatusWritten(success);
                break;
            }

            case OpcUA_QThread_Task::ReadFlowRate: {
            //读单个流量计
                int16_t result = 0;
                bool success = false;
                if (task.channel > 0 && task.channel <= s_flowmeterID_OnlyRead.size()) {
                    success = m_client->Open62541_ReadData(2, s_flowmeterID_OnlyRead[task.channel - 1], result);
                }
                emit signals_flowRateRead(task.channel, result, success);
                break;
            }

            case OpcUA_QThread_Task::ReadAllFlowRate: {
            //读所有流量计
                QVector<int16_t> result(s_flowmeterID_OnlyRead.size());
                QMap<QString, QVariant> data;
                bool success = m_client->Open62541_batchReading(2, s_flowmeterID_OnlyRead, data);
                if (success) {
                    for (int i = 0; i < s_flowmeterID_OnlyRead.size(); ++i) {
                        result[i] = data.value(s_flowmeterID_OnlyRead[i]).toInt();
                    }
                }
                emit signals_allFlowRateRead(result, success);
                break;
            }

            case OpcUA_QThread_Task::WriteFlowRate: {
            //写单个流量计
                bool success = false;
                if (task.channel > 0 && task.channel <= s_flowmeterID_ReadAndWrite.size()) {
                    success = m_client->Open62541_WriteData(2, s_flowmeterID_ReadAndWrite[task.channel - 1], task.int16Value);
                }
                emit signals_flowRateWritten(task.channel, success);
                break;
            }

            case OpcUA_QThread_Task::WriteAllFlowRate: {
            //写所有流量计
                bool success = true;
                int max = qMin(task.int16Vector.size(), s_flowmeterID_ReadAndWrite.size());
                for (int i = 0; i < max; ++i) {
                    if (!m_client->Open62541_WriteData(2, s_flowmeterID_ReadAndWrite[i], task.int16Vector[i])) {
                        success = false;
                        break;
                    }
                }
                emit signals_allFlowRateWritten(success);
                break;
            }

            case OpcUA_QThread_Task::ReadPressure: {
            //读单个压力值
                int16_t result = 0;
                bool success = false;
                if (task.channel > 0 && task.channel <= s_pressuremeterID_OnlyRead.size()) {
                    success = m_client->Open62541_ReadData(2, s_pressuremeterID_OnlyRead[task.channel - 1], result);
                }
                emit signals_pressureRead(task.channel, result, success);
                break;
            }

            case OpcUA_QThread_Task::ReadAllPressure: {
            //读所有压力值
                QVector<int16_t> result(s_pressuremeterID_OnlyRead.size());
                QMap<QString, QVariant> data;
                bool success = m_client->Open62541_batchReading(2, s_pressuremeterID_OnlyRead, data);
                if (success) {
                    for (int i = 0; i < s_pressuremeterID_OnlyRead.size(); ++i) {
                        result[i] = data.value(s_pressuremeterID_OnlyRead[i]).toInt();
                    }
                }
                emit signals_allPressureRead(result, success);
                break;
            }

            case OpcUA_QThread_Task::WritePressure: {
            //写单个压力值
                bool success = false;
                if (task.channel > 0 && task.channel <= s_pressuremeterID_ReadAndWrite.size()) {
                    success = m_client->Open62541_WriteData(2, s_pressuremeterID_ReadAndWrite[task.channel - 1], task.int16Value);
                }
                emit signals_pressureWritten(task.channel, success);
                break;
            }

            case OpcUA_QThread_Task::WriteAllPressure: {
            //写所有压力值
                bool success = true;
                int max = qMin(task.int16Vector.size(), s_pressuremeterID_ReadAndWrite.size());
                for (int i = 0; i < max; ++i) {
                    if (!m_client->Open62541_WriteData(2, s_pressuremeterID_ReadAndWrite[i], task.int16Vector[i])) {
                        success = false;
                        break;
                    }
                }
                emit signals_allPressureWritten(success);
                break;
            }

            case OpcUA_QThread_Task::ReadMKS: {
            //读mks
                int16_t pressure = 0, opening = 0;
                bool success = m_client->Open62541_ReadData(2, s_MKSID_OnlyRead[0], pressure) &&
                               m_client->Open62541_ReadData(2, s_MKSID_OnlyRead[1], opening);
                emit signals_mksRead(pressure, opening, success);
                break;
            }

            case OpcUA_QThread_Task::WriteMKS: {
            //写mks
                bool success = false;
                if (s_MKSID_ReadAndWrite.size() >= 3) {
                    success = m_client->Open62541_WriteData(2, s_MKSID_ReadAndWrite[0], task.mksPressure) &&
                              m_client->Open62541_WriteData(2, s_MKSID_ReadAndWrite[1], task.mksOpening) &&
                              m_client->Open62541_WriteData(2, s_MKSID_ReadAndWrite[2], task.mksMode);
                }
                emit signals_mksWritten(success);
                break;
            }

            case OpcUA_QThread_Task::ReadTemperature: {
            //读温度
                int16_t t1 = 0, t2 = 0;
                bool success = m_client->Open62541_ReadData(2, s_temperatureID_OnlyRead[0], t1) &&
                               m_client->Open62541_ReadData(2, s_temperatureID_OnlyRead[1], t2);
                emit signals_temperatureRead(t1, t2, success);
                break;
            }

            case OpcUA_QThread_Task::WriteTemperature: {
            //写温度
                bool success = false;
                if (s_temperatureID_ReadAndWrite.size() >= 3) {
                    success = m_client->Open62541_WriteData(2, s_temperatureID_ReadAndWrite[0], task.tempStatus1) &&
                              m_client->Open62541_WriteData(2, s_temperatureID_ReadAndWrite[1], task.tempStatus2) &&
                              m_client->Open62541_WriteData(2, s_temperatureID_ReadAndWrite[2], task.temp1) &&
                              m_client->Open62541_WriteData(2, s_temperatureID_ReadAndWrite[3], task.temp2);
                }
                emit signals_temperatureWritten(success);
                break;
            }

            case OpcUA_QThread_Task::ReadOtherValveStatus: {
            //读取其他bool值
                bool result = false;
                bool success = false;
                if (task.channel > 0 && task.channel <= s_otherValveID_OnlyRead.size()) {
                    success = m_client->Open62541_ReadData(2, s_otherValveID_OnlyRead[task.channel - 1], result);
                }
                emit signals_otherValveStatusRead(task.channel, result, success);
                break;
            }

            case OpcUA_QThread_Task::ReadOtherDataValue: {
            //读取其他int值
                int16_t result = 0;
                bool success = false;
                if (task.channel > 0 && task.channel <= s_otherDataID_OnlyRead.size()) {
                    success = m_client->Open62541_ReadData(2, s_otherDataID_OnlyRead[task.channel - 1], result);
                }
                emit signals_otherDataValueRead(task.channel, result, success);
                break;
            }

            //定制化读取，以下还需要修改，到底需要读那些值
            case OpcUA_QThread_Task::readDataToHomePage: {
                //读流量
                QVector<int16_t> result1(s_flowmeterID_OnlyRead.size());
                QMap<QString, QVariant> data1;
                bool success1 = m_client->Open62541_batchReading(2, s_flowmeterID_OnlyRead, data1);
                if (success1) {
                    for (int i = 0; i < s_flowmeterID_OnlyRead.size(); ++i) {
                        result1[i] = data1.value(s_flowmeterID_OnlyRead[i]).toInt();
                    }
                }
                //读压力
                QVector<int16_t> result2(s_pressuremeterID_OnlyRead.size());
                QMap<QString, QVariant> data2;
                bool success2 = m_client->Open62541_batchReading(2, s_pressuremeterID_OnlyRead, data2);
                if (success2) {
                    for (int i = 0; i < s_pressuremeterID_OnlyRead.size(); ++i) {
                        result2[i] = data2.value(s_pressuremeterID_OnlyRead[i]).toInt();
                    }
                }
                //读电磁阀
                QVector<bool> result3(s_valveID_OnlyRead.size());
                QMap<QString, QVariant> data3;
                bool success3 = m_client->Open62541_batchReading(2, s_valveID_OnlyRead, data3);
                if (success3) {
                    for (int i = 0; i < s_valveID_OnlyRead.size(); ++i) {
                        result3[i] = data3.value(s_valveID_OnlyRead[i]).toBool();
                    }
                }
                //读温度
                QVector<int16_t> result4(s_temperatureID_OnlyRead.size());
                QMap<QString, QVariant> data4;
                bool success4 = m_client->Open62541_batchReading(2, s_temperatureID_OnlyRead, data4);
                if (success4) {
                    for (int i = 0; i < s_temperatureID_OnlyRead.size(); ++i) {
                        result4[i] = data4.value(s_temperatureID_OnlyRead[i]).toInt();
                    }
                }
                //读实数压力
                int32_t result5 = 0;
                bool success5 = m_client->Open62541_ReadData(2, u8"omron.nj101.压力实数9", result5);
                emit signals_sendDataToHomePage(result1, result2, result3, result4, result5, success1 && success2 && success4 && success3 && success5);
                break;
            }

            //定制型写入操作，HomePage专用
            case OpcUA_QThread_Task::WriteDataFromHomePage: {
                //写入所有流量
                bool flowSuccess = true;
                int flowMax = qMin(task.int16Vector.size(), s_flowmeterID_ReadAndWrite.size());
                for (int i = 0; i < flowMax; ++i) {
                    if (!m_client->Open62541_WriteData(2, s_flowmeterID_ReadAndWrite[i], task.int16Vector[i])) {
                        flowSuccess = false;
                        break;
                    }
                }

                //写入所有压力
                bool pressureSuccess = true;
                int pressureMax = qMin(task.int16Vector2.size(), s_pressuremeterID_ReadAndWrite.size());
                for (int i = 0; i < pressureMax; ++i) {
                    if (!m_client->Open62541_WriteData(2, s_pressuremeterID_ReadAndWrite[i], task.int16Vector2[i])) {
                        pressureSuccess = false;
                        break;
                    }
                }

                //写入所有阀门
                bool valveSuccess = true;
                int valveMax = qMin(task.boolVector.size(), s_valveID_ReadAndWrite.size());
                for (int i = 0; i < valveMax; ++i) {
                    if (!m_client->Open62541_WriteData(2, s_valveID_ReadAndWrite[i], task.boolVector[i])) {
                        valveSuccess = false;
                        break;
                    }
                }

                //触发结果信号
                emit signals_dataFromHomePageWritten(flowSuccess && pressureSuccess && valveSuccess);
                break;
            }
        }
    }
}
